package com.teziyou.commodity.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jodd.bean.BeanUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mytravel.commodityBridge.service.bean.Distributor;
import com.mytravel.commodityBridge.service.bean.TicketAds;
import com.mytravel.commodityBridge.service.bean.TicketInfo;
import com.mytravel.platform.bean.AdministrativeBean;
import com.teziyou.commodity.bean.Page;
import com.teziyou.commodity.enumtype.EnableEnum;
import com.teziyou.commodity.enumtype.ShelfStatusEnum;
import com.teziyou.commodity.service.AreaService;
import com.teziyou.commodity.service.SceneService;
import com.teziyou.commodity.service.TicketService;
import com.teziyou.commodity.util.DateUtil;
import com.teziyou.commodity.util.Lang;
import com.teziyou.commodity.util.Lists;
import com.teziyou.commodity.util.Maps;

@SuppressWarnings({"rawtypes","unchecked"})
@Service
public class TicketLogic {
	
	@Autowired
	private TicketService ticketService;
	@Autowired
	private SceneService sceneService;
	@Autowired
	private AreaService areaService;
	
	public Page listTickets(Map<String, String> paramMap) {
		String name = Lang.toString(paramMap.get("name"));
		if(StringUtils.isNotBlank(name))
			paramMap.put("name", "%"+name+"%");
		List ticketList = ticketService.findTicketsByPage(paramMap);
		Page page = new Page();
		if(ticketList!=null) {
			Iterator iter = ticketList.iterator();
			while(iter.hasNext()) {
				Map ticket = (Map)iter.next();
				long sceneId = Lang.toInt(ticket.get("scenicId"));
				Map scene = sceneService.findSceneById(sceneId);
				ticket.put("scene", scene);
				ticket.put("fromTime_display", DateUtil.formatDate(Lang.toString(ticket.get("fromTime")), DateUtil.pattern2, DateUtil.pattern3));
				ticket.put("endTime_display", DateUtil.formatDate(Lang.toString(ticket.get("endTime")), DateUtil.pattern2, DateUtil.pattern3));
				ticket.put("isOK_display", EnableEnum.getEnumByCode(Lang.toInt(ticket.get("isok"))).getDesc());
			}
			int count = ticketService.findTicketsByPageCount(paramMap);
			System.out.println(count);
			int pageNo = Lang.toInt(paramMap.get("pageNo"));
			page = new Page(pageNo, count, ticketList);
		}
		return page;
	}
	
	public List loadTickets(String ids) {
		List list = Lists.newArrayList();
		if(StringUtils.isNotBlank(ids)) {
			String[] idStrs = ids.split(",");
			for(String id:idStrs) {
				if(StringUtils.isNotBlank(id)) {
					Map ticket = null;//ticketService.findTicketById(Long.parseLong(id));
					long sceneId = Lang.toInt(ticket.get("scenic_id"));
					Map scene = sceneService.findSceneById(sceneId);
					ticket.put("scene_name", scene.get("name"));
					ticket.put("isOK_display", EnableEnum.getEnumByCode(Lang.toInt(ticket.get("isOK"))).getDesc());
					list.add(ticket);
				}
			}
		}
		return list;
	}

	public void saveRelations(Map<String, String> paraMap) {
		
	}

	public void addTicket(Map<String, String> param) {
		TicketInfo ti = new TicketInfo();
		param.remove("scene-name-display");
		param.remove("distributor-name-display");
		param.remove("distributor_id");
		System.err.println(param);
		param.put("ticketInfoId", "0");
		BeanUtil.populateBean(ti, param);
		ticketService.addTicketInfo(ti);
	}
	
	public void addDistributor(Map<String, String> param) {
		String rc = Lang.toString(param.get("registeredCapital"));
		if(StringUtils.isBlank(rc))
			param.put("registeredCapital", "0");
		Distributor db = new Distributor();
		BeanUtil.populateBean(db, param);
		ticketService.addDistributor(db);
	}
	
	public Page findDistributors(Map<String, String> paramMap) {
		String name = Lang.toString(paramMap.get("name"));
		if(StringUtils.isNotBlank(name))
			paramMap.put("name", "%"+name+"%");
		List distributorList = ticketService.findDistributorPage(paramMap);
		Page page = new Page();
		if(distributorList!=null) {
			Iterator iter = distributorList.iterator();
			while(iter.hasNext()) {
				Map distributor = (Map)iter.next();
				System.err.println(distributor);
				String countyId = Lang.toString(distributor.get("admDivId"));
				int provinceId = Lang.toInt(countyId.substring(0, 6));
				int cityId = Lang.toInt(countyId.substring(0, 8));
				AdministrativeBean province = areaService.getAdministrativeById(provinceId);
				AdministrativeBean city = areaService.getAdministrativeById(cityId);
				distributor.put("province", province);
				distributor.put("city", city);
				distributor.put("isok_display", EnableEnum.getEnumByCode(
						Lang.toInt(distributor.get("isok"))).getDesc());
			}
			int count = ticketService.findDistributorCount(paramMap);
			int pageNo = Lang.toInt(paramMap.get("pageNo"));
			page = new Page(pageNo, count, distributorList);
		}
		return page;
	}

	public void deleteTickets(String[] ticketIds) {
		for(String id:ticketIds) {
			ticketService.deleteTicketById(Long.parseLong(id));
		}
	}
	
	public Page findOnSaleTickets(Map<String, String> paramMap) {
		Page page = new Page();
		List onsaleTickets = ticketService.findOnSaleTicketByPage(paramMap);
		Iterator iter = onsaleTickets.iterator();
		while(iter.hasNext()) {
			Map onsaleTicket = (Map)iter.next();
			System.err.println(onsaleTicket);
			long scenicId = Lang.toLong(onsaleTicket.get("scenic_id"));
			Map scene = sceneService.findSceneById(scenicId);
			onsaleTicket.put("scene", scene);
			onsaleTicket.put("shelf_status", ShelfStatusEnum.getEnumByCode(Lang.toInt(onsaleTicket.get("isOK"))).getDesc());
			onsaleTicket.put("launch_time_display", DateUtil.formatDate(new Date(Lang.toLong(onsaleTicket.get("launch_time"))), "yyyy.MM.dd"));
			onsaleTicket.put("from_time_display", DateUtil.formatDate(new Date(Lang.toLong(onsaleTicket.get("from_time"))), "yyyy.MM.dd"));
			onsaleTicket.put("end_time_display", DateUtil.formatDate(new Date(Lang.toLong(onsaleTicket.get("end_time"))), "yyyy.MM.dd"));
			onsaleTicket.put("discount_display", Lang.toString(onsaleTicket.get("discount")).replace("0", ""));
		}
		int count = ticketService.findOnSaleTicketByPageCount(paramMap);
		//int count = 10;
		int pageNo = Lang.toInt(paramMap.get("pageNo"));
		page = new Page(pageNo, count, onsaleTickets);
		return page;
	}
	
	public Map findTicketById(long id) {
		//System.out.println(id);
		Map ticket = ticketService.findTicketInfoStrById(id);
		ticket.put("fromTime_display", DateUtil.formatDate(Lang.toString(ticket.get("fromTime")), DateUtil.pattern2, DateUtil.pattern3));
		ticket.put("endTime_display", DateUtil.formatDate(Lang.toString(ticket.get("endTime")), DateUtil.pattern2, DateUtil.pattern3));
		long sceneId = Lang.toLong(ticket.get("scenicId"));
		Map scene = sceneService.findSceneById(sceneId);
		List<String> imgs = sceneService.getImageListByMap(Maps.newHashMap("id", sceneId+""));
		scene.put("imgs", imgs);
		ticket.put("scene", scene);
		//System.out.println(sceneService.getImageListStringByMap(Maps.newHashMap("id", sceneId+"")));
		//System.out.println(ticket);
		//ticket.put("from_time_display", );
		return ticket;
	}
	
	public List findRelateTicketsByTicketId(long id) {
		List tickets = ticketService.getRelatedTicketsByTicketId(id);
		//System.out.println(tickets);
		if(!CollectionUtils.isEmpty(tickets)) {
			Iterator iter = tickets.iterator();
			while(iter.hasNext()) {
				Map ticket = (Map) iter.next();
				long sceneId = Lang.toLong(ticket.get("scenic_id"));
				Map scene = sceneService.findSceneById(sceneId);
				ticket.put("scene", scene);
				ticket.put("from_time_display", DateUtil.formatDate(new Date(Lang.toLong(ticket.get("from_time"))), DateUtil.pattern3));
				ticket.put("end_time_display", DateUtil.formatDate(new Date(Lang.toLong(ticket.get("end_time"))), DateUtil.pattern3));
			}
		}
		return tickets;
	}
	
	public void bindRelateTickets(long id, String ticketIdStr) {
		String[] ticketIds = ticketIdStr.split(",");
		List<Map<String, String>> relationList = new ArrayList<>();
		for(String ticketId:ticketIds) {
			Map<String, String> map = new HashMap<>();
			map.put("ticket_info_id", id+"");
			map.put("Tic_ticket_info_id", ticketId);
			relationList.add(map);
		}
		//System.err.println(id);
		//System.err.println(relationList);
		ticketService.bindTicketTicketRelation(relationList);
	}

	public void deleteTicketTicketRelation(Map<String, String> paraMap) {
		ticketService.delRelatedTicketDetail(paraMap);
	}

	public Map loadTicketById(long id) {
		Map ticket = ticketService.findTicketInfoStrById(id);
		
		ticket.put("fromTime_display", DateUtil.formatDate(Lang.toString(ticket.get("fromTime")), DateUtil.pattern2, DateUtil.pattern3));
		ticket.put("endTime_display", DateUtil.formatDate(Lang.toString(ticket.get("endTime")), DateUtil.pattern2, DateUtil.pattern3));
		long sceneId = Lang.toLong(ticket.get("scenicId"));
		Map scene = sceneService.findSceneById(sceneId);
		ticket.put("scene", scene);
		long distributorId = Lang.toLong(ticket.get("distributorId"));
		Map distributor = ticketService.getDistributorStrById(distributorId);
		ticket.put("distributor", distributor);
		System.err.println(ticket);
		return ticket;
	}
	
	public void updateTicket(Map<String, String> param) {
		TicketInfo ti = new TicketInfo();
		param.remove("scene-name-display");
		param.remove("distributor-name-display");
		param.remove("distributor_id");
		BeanUtil.populateBean(ti, param);
		ticketService.editTicketInfo(ti);
	}
	
	public Page findAds(Map<String, String> paramMap) {
		Page page = new Page();
		List ads = ticketService.findTicketAdsStrByPage(paramMap);
		if(!CollectionUtils.isEmpty(ads)) {
			Iterator iter = ads.iterator();
			while(iter.hasNext()) {
				Map ad = (Map)iter.next();
				System.err.println(ad);
				
				long scenicId = Lang.toLong(ad.get("scenicId"));
				Map scene = sceneService.findSceneById(scenicId);
				System.err.println(scene);
				ad.put("scene", scene);
				ad.put("isok_display", ShelfStatusEnum.getEnumByCode(Lang.toInt(ad.get("isok"))).getDesc());
				ad.put("launch_time_display", DateUtil.formatDate(Lang.toString(ad.get("launchTime")), DateUtil.pattern2, DateUtil.pattern3));
				ad.put("from_time_display", DateUtil.formatDate(Lang.toString(ad.get("fromTime")), DateUtil.pattern2, DateUtil.pattern3));
				ad.put("end_time_display", DateUtil.formatDate(Lang.toString(ad.get("endTime")), DateUtil.pattern2, DateUtil.pattern3));
				/*onsaleTicket.put("shelf_status", ShelfStatusEnum.getEnumByCode(Lang.toInt(onsaleTicket.get("isOK"))).getDesc());
				onsaleTicket.put("launch_time_display", DateUtil.formatDate(new Date(Lang.toLong(onsaleTicket.get("launch_time"))), "yyyy.MM.dd"));
				onsaleTicket.put("from_time_display", DateUtil.formatDate(new Date(Lang.toLong(onsaleTicket.get("from_time"))), "yyyy.MM.dd"));
				onsaleTicket.put("end_time_display", DateUtil.formatDate(new Date(Lang.toLong(onsaleTicket.get("end_time"))), "yyyy.MM.dd"));
				onsaleTicket.put("discount_display", Lang.toString(onsaleTicket.get("discount")).replace("0", ""));*/
			}
		}
		int count = ticketService.findTicketAdsByPageCount(paramMap);
		//int count = 10;
		int pageNo = Lang.toInt(paramMap.get("pageNo"));
		page = new Page(pageNo, count, ads);
		return page;
	}

	public void saveAd(Map map) {
		TicketAds ta = new TicketAds();
		map.remove("scene-name-display");
		map.remove("distributor-name-display");
		map.remove("distributor_id");
		BeanUtil.populateBean(ta, map);
		ta.setLaunchTime(DateUtil.formatDate(new Date(), DateUtil.pattern1));
		ticketService.addTicketAds(ta);
	}
	
	public void updateAd(Map map) {
		TicketAds ta = new TicketAds();
		BeanUtil.populateBean(ta, map);
		ticketService.updateTicketAds(ta);
	}

}
